import { isDom } from "./tool/is";
import { EventName } from "./enum/eventEnum";
import { Point2d } from "./core/point";
import { EventCallback, ShapeEvent } from "./types/event";
import { Shape } from "./graphic/shape/shape";
import { Graphic } from "./graphic/type";
import DataStorage from "./dataStorage";
import { Painter } from "./core/painter";
import HandlerProxy from "./core/handlerProxy";
import Handler from "./handler";
import { Layer } from "./core/layer";
import { NodeType } from "./enum/NodeTypeEnum";
export const move = "mousemove";
export const click = "mousedown";
export interface IGrenderOps {
	width: number;
	height: number;
}
export class Grender {
	allShapes: DataStorage;
	dataStorage: DataStorage;
	// ctx: CanvasRenderingContext2D;
	painter: Painter;
	handler: Handler;
	root:HTMLElement;
	options: IGrenderOps
	children:Layer[]
	constructor(root: HTMLCanvasElement, optins: IGrenderOps) {
		this.init(root, optins);
	}
	private init(root: HTMLCanvasElement, options: IGrenderOps) {
		if (!root || !isDom(root)) {
			throw new Error("请传入DOM签");
		}
		this.root=root;
		this.options = options;
		//存储图形容器
		this.dataStorage = new DataStorage();
		
	}
	// add(shape: Graphic) {
	// 	if(shape.bindRender){
	// 		throw new Error("改元素已经成功绑定了render上下文");
	// 	}
	// 	shape.bingContext(this);
	// 	this.dataStorage.addShape(shape);
	// 	this.refresh();
	// 	shape.renderQueue.executeQueue();
	// }
	add(layer:Layer){
		if(layer.type !== NodeType.LAYER){
			throw new Error("当前元素必须为图层");
		}
		const {root,options,dataStorage} = this;
		const painter = new Painter(root, options, dataStorage,layer);
		const handlerProxy = new HandlerProxy(
			root,
			painter.getContainer(),
			dataStorage
		);
		layer.addLayerToAttributes({painter,});
		const handler = new Handler({
			handlerProxy,
			storage: dataStorage,
			layer,
		});
		layer.addLayerToAttributes({handler});

		this.dataStorage.addLayer(layer);
		layer.parent = this;
		this.children = this.dataStorage.getData();
	}
	remove(layer:Layer){
		this.dataStorage.delLayer(layer);
		layer.destory();
	}
	//注意 这里的刷新是指对所有的图层的元素重新绘制
	refresh() {
		//TODO 处理刷新 1.如果为group处理，2如果为layer处理 3 如果为普通Shape处理
		const layerList = this.dataStorage.getData();
		console.log(layerList)
		layerList.forEach(layer =>{
			layer.painter.clear();
			layer.painter.refresh();
		})
	}
}
